package pers.w.chaptertalk.reader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.List;
import java.util.Map;

import pers.w.chaptertalk.R;
import pers.w.chaptertalk.funs.BookInfo;
import pers.w.chaptertalk.funs.ChapterInfo;
import pers.w.chaptertalk.funs.db.Tab_Chapter;

//侧边栏封装
class SideBar {
    private LinearLayout layout;
    ListView lv_chapters;
    private  Button btn_backReader;
    private  TextView tv_reading_bookName;
    private  ReaderActivity reader;
    SimpleAdapter adapter;

    void setVisibility(boolean visibility) {
        layout.setVisibility(visibility ? View.VISIBLE : View.GONE);


        if (visibility)
            layout.startAnimation(reader.ltoC);
        else layout.startAnimation(reader.ctoL);
    }

    boolean isVisibility() {
        return layout.getVisibility() == View.VISIBLE;
    }

    @SuppressLint("InflateParams")
    SideBar(ReaderActivity reader) {
        this.reader = reader;

        LayoutInflater inflater = LayoutInflater.from(reader);
        layout = inflater.inflate(R.layout.reader_sidebar, null).findViewById(R.id.chapters_sidebar);
        layout.setClickable(true);
        layout.setFocusable(true);
        ConstraintLayout cl = reader.findViewById(R.id.cl_reader);
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(0, 1, 1, 1);
        layout.setVisibility(View.GONE);//开局隐藏
        cl.addView(layout, lp);

        lv_chapters = layout.findViewById(R.id.lv_chapters);
        btn_backReader = layout.findViewById(R.id.btn_backReader);
        tv_reading_bookName = layout.findViewById(R.id.tv_reading_bookName);
    }
    static class MenuSimpleAdapter extends SimpleAdapter {
        private BookInfo book;
        private List<Map<String, String>> mData;
        MenuSimpleAdapter(Context context, List<Map<String, String>> data,  BookInfo book) {
            super(context, data, R.layout.book_menu,
                    new String[]{Tab_Chapter.CHAPTERNAME}, new int[]{R.id.tv_chapterName});
            this.book =book;
            mData = data;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = super.getView(position, convertView, parent);
            if (v != null) {
                ImageView cache = v.findViewById(R.id.iv_cache);

                ChapterInfo chapter = new ChapterInfo(book.getBookDir(), mData.get(position));
                if (chapter.chapterFile.exists())
                    cache.setBackgroundResource(android.R.drawable.checkbox_on_background);
                else cache.setBackground(null);
            }
            return v;
        }


    }
    void initSidebar() {
        tv_reading_bookName.setText(reader.book.getFieldValue(R.string.info_name));

        adapter = new MenuSimpleAdapter(reader, reader.chapters, reader.book);
        lv_chapters.setAdapter(adapter);
        lv_chapters.setOnItemClickListener((parent, view, position, id) -> {
            if (reader.currentChapterIndex != position) {
                //这里并不是真的下一页，而是通过true这个参数，就会加载章节开头
                reader.loading_chapter(position, ReaderControl.BEGIN);
            }
            setVisibility(false);
        });
        btn_backReader.setOnClickListener(v -> setVisibility(false));

    }
}